package com.huiztech.watersoil.activity;

import java.text.DecimalFormat;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.res.Resources;
import android.graphics.Color;
import android.graphics.drawable.Drawable;
import android.opengl.Visibility;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.WindowManager;
import android.view.View.OnClickListener;
import android.view.Window;
import android.view.inputmethod.InputMethodManager;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ExpandableListAdapter;
import android.widget.ExpandableListView;
import android.widget.ExpandableListView.OnGroupCollapseListener;
import android.widget.ExpandableListView.OnGroupExpandListener;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.Spinner;
import android.widget.Toast;

import com.huiztech.watersoil.R;
import com.huiztech.watersoil.R.array;
import com.huiztech.watersoil.R.drawable;
import com.huiztech.watersoil.R.id;
import com.huiztech.watersoil.R.layout;
import com.huiztech.watersoil.model.Stream;
import com.huiztech.watersoil.utils.Convertor;
import com.huiztech.watersoil.utils.DataAdapter;
import com.huiztech.watersoil.view.PopMenu;
/**
 * 流域详情页面
 * @author alin
 *
 */

public class DetailActivity extends Activity {  
    private ExpandableListView expandableListview;  
    private ExpandableListAdapter adapter;
    private Spinner factorBaseSpinner;
	private Button confirmButton;
	private Button viewMoreButton;
	private ImageButton helpButton;
	private ImageButton returnButton;
	private LinearLayout searchFrame;
	private EditText factorEdit;
	private ImageView arrowImage;
	
	private PopMenu popMenu;
	private Context context;
	
	private int factorBaseValue=0; //默认按日搜素
	
	private Stream stream;
	
	
    //初始化一级目录     
    private String[] group = new String[]{ 
    		"现状侵蚀"};       
    
    //构造二位数组存放数据内容
    private String[][] children = new String[1][2];
    
      
    public void onCreate(Bundle savedInstanceState) {  
        super.onCreate(savedInstanceState);  

        Resources res = getResources();
        Drawable drawable = res.getDrawable(R.drawable.bgcolor);
        this.getWindow().setBackgroundDrawable(drawable);
		
		requestWindowFeature(Window.FEATURE_CUSTOM_TITLE);
		setContentView(R.layout.activity_main);
		getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE, R.layout.title3);
 
        setContentView(R.layout.detail);  

    	context = DetailActivity.this;
		WindowManager wm = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
		popMenu = new PopMenu(context,wm.getDefaultDisplay().getWidth());
        
        
		searchFrame= (LinearLayout)findViewById(R.id.search_frame);
		searchFrame.setOnClickListener(new OnClickListener(){

			@Override
			public void onClick(View arg0) {
				// TODO Auto-generated method stub
				factorEdit.setFocusable(true);
			} 	
        });
		factorEdit = (EditText)findViewById(R.id.factor_edit);
		String text = "         降雨值(例如300)";
		confirmButton = (Button)findViewById(R.id.confirm_button);
		confirmButton.setOnClickListener(new clickView());
		viewMoreButton = (Button)findViewById(R.id.view_more_button);
		viewMoreButton.setOnClickListener(new clickViewMore());
		returnButton = (ImageButton)findViewById(R.id.return_button);
		returnButton.setOnClickListener(new clickReturn());
        
        
	    LayoutInflater inflater = getLayoutInflater();
	    View view=inflater.inflate(R.layout.detail_listview_group_item, null);
	    arrowImage = (ImageView)view.findViewById(R.id.arrow_image);
	    arrowImage.setOnClickListener(new clickArrow());
        
        helpButton = (ImageButton)findViewById(R.id.help_button);
        helpButton.setOnClickListener(new clickHelp());
        factorBaseSpinner=(Spinner)this.findViewById(R.id.factor_base_spinner);
		
		stream = (Stream)getIntent().getSerializableExtra("stream");
		
	    //下拉菜单
	    ArrayAdapter<CharSequence> factor_base_adapter = 
	    		ArrayAdapter.createFromResource(
	    				this, 
	    				R.array.factor_base_array, 
	    				R.layout.spinnerlayout
	    		);
	    factor_base_adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
	    
	    factorBaseSpinner.setAdapter(factor_base_adapter);
	    factorBaseSpinner.setPrompt("请选择计算类型");
	    factorBaseSpinner.setOnItemSelectedListener(new clickFactorBaseSpinner());
	    
		
		for(int i = 0;i<1;i++){
			children[i][0] = "请输入降雨值";
			children[i][1] = "";
		}
        	
		expandableListview= (ExpandableListView) findViewById(R.id.expandablelistview);  
		expandableListview.setGroupIndicator(null);
		expandableListview.setCacheColorHint(Color.TRANSPARENT);
		expandableListview.setDivider(null);//设置分割线
        adapter = new DataAdapter(this,group,children);  
        expandableListview.setAdapter(adapter);
        expandableListview.expandGroup(0);
        //分组展开  
        expandableListview.setOnGroupExpandListener(new OnGroupExpandListener(){  
            public void onGroupExpand(int groupPosition) {  
            }  
        });  
        //分组关闭  
        expandableListview.setOnGroupCollapseListener(new OnGroupCollapseListener(){  
            public void onGroupCollapse(int groupPosition) {  
            }  
        });  
        
    }  
    class clickArrow implements OnClickListener{

		@Override
		public void onClick(View v) {
			// TODO Auto-generated method stub
			Toast.makeText(getApplicationContext(),"haa",Toast.LENGTH_SHORT).show();
			System.out.println("test");
			expandableListview.expandGroup(0);
		}
    	
    }
    
	class clickView implements OnClickListener{
		@Override
		public void onClick(View v) {
			
			if(!factorEdit.getText().toString().equals("")){
				float factor = Float.parseFloat(factorEdit.getText().toString());
				
				Convertor convertor = new Convertor();
				convertor.convertByX(factor,factorBaseValue,stream);
				
				DecimalFormat format2 = new DecimalFormat("#0.00");// 设置小数点后2位
				
				children[0][0] = "单位面积侵蚀量: "+format2.format(stream.getY0()).toString()+" ";
				children[0][1] = "侵蚀量: "+format2.format(stream.getM0()).toString()+" ";
				int [] expandlist = new int [11];
				for(int i=0;i<10;i++){
					if(expandableListview.isGroupExpanded(i)){
						expandlist[i] = 1;
					}else{
						expandlist[i] = 0;
					}
				}
				expandableListview.setAdapter(adapter); 
				expandableListview.expandGroup(0);
				viewMoreButton.setVisibility(View.VISIBLE);

				Toast.makeText(getApplicationContext(),"查询完毕",Toast.LENGTH_SHORT).show();
				InputMethodManager imm = (InputMethodManager) DetailActivity.this.getSystemService(Context.INPUT_METHOD_SERVICE);
		        imm.hideSoftInputFromWindow(v.getWindowToken(), 0);
			}else{
				expandableListview.collapseGroup(0);
				for(int i = 0;i<1;i++){
					children[i][0] = "请输入降雨值";
					children[i][1] = "";
				}
				expandableListview.expandGroup(0);
				viewMoreButton.setVisibility(View.GONE);
				Toast.makeText(getApplicationContext(),"请输入降雨值",Toast.LENGTH_SHORT).show();
			}
		}
	}
	
	 
	
	class clickHelp implements OnClickListener{
		@Override
		public void onClick(View v) {
			popMenu.setText(0);
			popMenu.showAsDropDown(v);
		}
	}
	
	
	public void clickHint(View v) {   
		int imageTag = Integer.parseInt(((ImageView)v).getTag().toString());
		
		switch(imageTag){
		case 1:
			popMenu.setText(1);
			break;
		default:
			popMenu.setText(1);
		}
		
		popMenu.showAsDropDown(v);
    }  
	
	public void clickDetailHelp(View v) {      
		popMenu.showAsDropDown(v);
      }  
	
	
	class clickViewMore implements OnClickListener{
		@Override
		public void onClick(View v) {
			Intent intent = new Intent();
			intent.putExtra("stream",stream);
			intent.setClass(DetailActivity.this,com.huiztech.watersoil.activity.MoreDetailActivity.class);
			startActivity(intent);	
		}
	}
	
	class clickReturn implements OnClickListener{
		@Override
		public void onClick(View v) {
			finish();
		}
	}
	
	class clickFactorBaseSpinner implements OnItemSelectedListener{

		@Override
		public void onItemSelected(AdapterView<?> adapterView, View view, int position,long id) {
			factorBaseValue = position;
			String selected = adapterView.getItemAtPosition(position).toString();
		}

		@Override
		public void onNothingSelected(AdapterView<?> arg0) {
			// TODO Auto-generated method stub
			
		}
	}
	
}  
